package com.lagou.homework3_1;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class SortDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "sort");
        // 指定程序jar的本地路径
        job.setJarByClass(SortDriver.class);
        //设置Mapper和Reducer类
        job.setMapperClass(SortMapper.class);
        job.setReducerClass(SortReducer.class);
        // 设置Mapper输出key value类型
        job.setMapOutputKeyClass(Number.class);
        job.setMapOutputValueClass(NullWritable.class);
        // 设置Reducer输出key value数据类型
        job.setOutputKeyClass(NullWritable.class);
        job.setOutputValueClass(Text.class);
        // 设置输入数据路径
        FileInputFormat.addInputPath(job, new Path(args[0]));
        // 设置输出数据路径
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        // reduce数量，默认也是1
        job.setNumReduceTasks(1);
        // 设置GroupingComparator
        job.setGroupingComparatorClass(SortGroupingComparator.class);
        // 提交作业
        boolean res = job.waitForCompletion(true);
        System.exit(res?0:1);
    }
}
